<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>用回调函数比较键名来计算数组的交集</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.array-intersect-ukey" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_intersect_ukey</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7)</p><p class="refpurpose"><span class="refname">array_intersect_ukey</span> &mdash; <span class="dc-title">用回调函数比较键名来计算数组的交集</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.array-intersect-ukey-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_intersect_ukey</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$array1</code></span>
   , <span class="methodparam"><span class="type">array</span> <code class="parameter">$array2</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$...</code></span>
   ], <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$key_compare_func</code></span>
   ) : <span class="type">array</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>array_intersect_ukey()</strong></span> 返回一个数组，该数组包含了所有出现在
   <code class="parameter">array1</code> 中并同时出现在所有其它参数数组中的键名的值。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.array-intersect-ukey-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">array1</code></dt>

     <dd>

      <p class="para">
       Initial array for comparison of the arrays.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">array2</code></dt>

     <dd>

      <p class="para">
       First array to compare keys against.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">...</code></dt>

     <dd>

      <p class="para">
       Variable list of array arguments to compare keys against.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">key_compare_func</code></dt>

     <dd>

      <p class="para">
       在第一个参数小于，等于或大于第二个参数时，该比较函数必须相应地返回一个小于，等于或大于 0 的整数。
      </p>
      <div class="methodsynopsis dc-description"><span class="methodname"><span class="replaceable">callback</span></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$a</code></span>, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$b</code></span> ) : <span class="type">int</span></div>

     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-intersect-ukey-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the values of <code class="parameter">array1</code> whose keys exist
   in all the arguments.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.array-intersect-ukey-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6035">
    <p><strong>Example #1 <span class="function"><strong>array_intersect_ukey()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">key_compare_func</span><span style="color: #007700">(</span><span style="color: #0000BB">$key1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key2</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$key1&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$key2</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if&nbsp;(</span><span style="color: #0000BB">$key1&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">$key2</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$array1&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'blue'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'red'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'green'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'purple'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$array2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'green'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'blue'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'yellow'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">7</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'cyan'&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_intersect_ukey</span><span style="color: #007700">(</span><span style="color: #0000BB">$array1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'key_compare_func'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(2) {
  [&quot;blue&quot;]=&gt;
  int(1)
  [&quot;green&quot;]=&gt;
  int(3)
}
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   上例中可以看到只有 <em>&#039;blue&#039;</em>
   和 <em>&#039;green&#039;</em> 两个键名出现在两个数组中，因此被返回。此外注意
   <em>&#039;blue&#039;</em> 和 <em>&#039;green&#039;</em>
   的值在两个数组中是不同的。但因为只检查键名，因此还是匹配。返回的值只是
   <code class="parameter">array1</code> 中的。
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.array-intersect-ukey-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="array_diff.html" class="function" rel="rdfs-seeAlso">array_diff()</a> - 计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="array_udiff.html" class="function" rel="rdfs-seeAlso">array_udiff()</a> - 用回调函数比较数据来计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="array_diff_assoc.html" class="function" rel="rdfs-seeAlso">array_diff_assoc()</a> - 带索引检查计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="array_diff_uassoc.html" class="function" rel="rdfs-seeAlso">array_diff_uassoc()</a> - 用用户提供的回调函数做索引检查来计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="array_udiff_assoc.html" class="function" rel="rdfs-seeAlso">array_udiff_assoc()</a> - 带索引检查计算数组的差集，用回调函数比较数据</span></li>
    <li class="member"><span class="function"><a href="array_udiff_uassoc.html" class="function" rel="rdfs-seeAlso">array_udiff_uassoc()</a> - 带索引检查计算数组的差集，用回调函数比较数据和索引</span></li>
    <li class="member"><span class="function"><a href="array_diff_key.html" class="function" rel="rdfs-seeAlso">array_diff_key()</a> - 使用键名比较计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="array_diff_ukey.html" class="function" rel="rdfs-seeAlso">array_diff_ukey()</a> - 用回调函数对键名比较计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="array_intersect.html" class="function" rel="rdfs-seeAlso">array_intersect()</a> - 计算数组的交集</span></li>
    <li class="member"><span class="function"><a href="array_intersect_assoc.html" class="function" rel="rdfs-seeAlso">array_intersect_assoc()</a> - 带索引检查计算数组的交集</span></li>
    <li class="member"><span class="function"><a href="array_intersect_uassoc.html" class="function" rel="rdfs-seeAlso">array_intersect_uassoc()</a> - 带索引检查计算数组的交集，用回调函数比较索引</span></li>
    <li class="member"><span class="function"><a href="array_intersect_key.html" class="function" rel="rdfs-seeAlso">array_intersect_key()</a> - 使用键名比较计算数组的交集</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>